# -*- coding: utf-8 -*-
"""
Coroutines and concurrency/Tornado Locks/test02.py
"""
import asyncio
from tornado.locks import Semaphore
from tornado import gen

semaphore = Semaphore(value=5)


async def worker(worker_id):
    await semaphore.acquire()
    try:
        r_message = f"Worker {worker_id} is working..."
        print(r_message)
        await gen.sleep(10)
    finally:
        r_message = f"Worker {worker_id} is work done off work...."
        print(r_message)
        semaphore.release()


async def main():
    await gen.multi([
        worker(worker_id) for worker_id in range(10)
    ])


if __name__ == '__main__':
    asyncio.run(main())
